<?php
namespace Admin\Controller;
use Think\Controller;
class FoodsController extends CommonController {
	//添加页面
    public function add(){
        //查询所有分类
        $Mclass = M('mvp_class');
        $cinfo = $Mclass->select();
        // var_dump($cinfo);
        $this->assign('cinfo',$cinfo);
    	$this->display();
    }
    //执行添加
    public function insert(){
    	//获取时间
        $_POST['addtime'] = time();
        // var_dump($_POST);die;

        // 处理图片
        if($_FILES['fpic']['error'] == 0){
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->rootPath = './Public';//手动设置文件上传根目录
            $upload->savePath  =      '/Uploads/Admin/Foods/'; // 设置附件上传目录    // 上传文件     
            $infos = $upload->upload();
            if(!$infos) {// 上传错误提示错误信息        
                $this->error($upload->getError(),'',1000000);    
            }else{// 上传成功        
                $_POST['fpic'] = ltrim($upload->rootPath,'.').$infos['fpic']['savepath'].$infos['fpic']['savename'];
            }
        }
    	//创建数据库
    	$Foods = M('mvp_foods');
    	//创建数据
    	$Foods->create();
    	//执行添加
    	$cid=$Foods->add();
        // echo $cid;
    	if($cid){
    		$this->success('添加成功',U('Admin/Foods/index'),3);
    	}else{
    		$this->success('添加失败',U('Admin/Foods/index'),3);

    	}

    }

    //分类列表页面
    public function index(){

    	//获取数据库
    	$Foods = M('mvp_foods');
     

        //获取关键字
        if(!empty($_GET['keywords'])){
            //建议使用数组形式来创建where条件
            // $where = "username like '%".$_GET['keyword']."%'";
            $where['name'] = array('like','%'.$_GET["keywords"].'%');
            // $where['username'] = array('eq','admin');
        }else{
            $where = "";
        }
        //获取总数
        $count = $Foods->where($where)->count();
        //获取每页显示的数量
        $num = !empty($_GET['num']) ? $_GET['num'] : 10;
        //实例化分页对象
        $page = new \Think\Page($count,$num);
        //获取limit参数
        $limit = $page->firstRow.','.$page->listRows;
        //获取页码信息(页码字符串)
        $pages = $page->show();

    	//获取数据
    	$info = $Foods->limit($limit)->field('mvp_foods.id,mvp_foods.name,mvp_foods.price,mvp_foods.fstatus,mvp_foods.addtime,mvp_class.cname,mvp_foods.number,mvp_foods.Specifications')->where($where)->join('mvp_class on mvp_foods.cid=mvp_class.id')->order('mvp_foods.id')->select();
        // echo $Foods->_sql();
        // var_dump($info);die;
        $this->assign('info',$info);
    	$this->assign('pages',$pages);
    	// var_dump($info);
    	$this->display();
    }

    public function edit(){
    	//获取要修改的id
    	$id = I('get.id');
    	// echo $id;
    	//查询数据
    	$Foods = M('mvp_foods');
    	$info = $Foods->where('id='.$id)->select();
        // var_dump($info);die;
        //查出所有的分类
        $Mclass = M('mvp_class');
        $cinfo = $Mclass->select();
    	// var_dump($info);
    	// 分配
        $this->assign('info',$info);
    	$this->assign('cinfo',$cinfo);
    	//展示要修改的类
    	$this->display();
    }

    public function update(){
    	// var_dump($_FILES);die;
    	//查询数据
    	$Foods = M('mvp_foods');
        // var_dump($Foods);die;
    	
        //处理img字段
        if($_FILES['fpic']['error'] == 0){
            $upload = new \Think\Upload();// 实例化上传类    
            $upload->maxSize   =     3145728 ;// 设置附件上传大小    
            $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
            $upload->rootPath = './Public';//手动设置文件上传根目录
            $upload->savePath  =      '/Uploads/Admin/Foods/'; // 设置附件上传目录    // 上传文件     
            $infos = $upload->upload();
            if(!$infos) {// 上传错误提示错误信息        
                $this->error($upload->getError(),'',1000000);    
            }else{// 上传成功        
                $_POST['fpic'] = ltrim($upload->rootPath,'.').$infos['fpic']['savepath'].$infos['fpic']['savename'];
            }
            //获取原图的路径
            $r = $Foods->find($_POST['id']);
            $path = '/mvp'.$r['fpic'];
            // echo $path;die;
            //删除该图片  /Public/Uploads/2015-09-25/5604e73313a07.jpg
            unlink('.'.$path);
        }
        // var_dump($_POST);die;
        $Foods->create();
        $num = $Foods->save();
         // echo $num;
        // echo $Mclass->_sql();die;
    	if($num == 1){
    		$this->success('修改成功',U('Admin/Foods/index'),3);
    	}else{
    		$this->error('修改失败',U('Admin/Foods/index'),3);

    	}
    }

    //删除操作
    public function delete(){
    	$id=I('get.id');
        // echo $id;
        //判断美食中是否有优惠
        $Sell = M('mvp_sell');
        $finfo = $Sell->where('fid='.$id)->select();
        // var_dump($finfo);
        if(!empty($finfo)) {$this->error('美食中存在优惠,请先删除优惠',U('Admin/Sell/index'),3);die;}
        //判断美食中是否有评论
        $Comment = M('mvp_comment');
        $cinfo = $Comment->where('fid='.$id)->select();
        if(!empty($cinfo)) {$this->error('美食中存在评论,请先删除评论',U('Admin/Comment/index'),3); die;}
        //判断美食中是否存在日志
        $Logs = M('mvp_logs');
        $linfo = $Logs->where('fid='.$id)->select();
        if(!empty($cinfo)) {$this->error('美食中存在评论,请先删除评论',U('Admin/Comment/index'),3); die;}

    	// echo $id;
    	$Foods = M('mvp_foods');
    	$num = $Foods->where('id='.$id)->delete();
    	// var_dump($num);
    	if($num == 1){
    		$this->success('删除成功',U('Admin/Foods/index'),3);
    	}else{
    		$this->error('删除失败',U('Admin/Foods/index'),3);

    	}

    }


}